using System;
using System.Diagnostics.Contracts;

namespace OpenWaves.ImageTransformations.Web.Controls
{
    public static class ImageTransformationTypeExtensions
    {
        public static IImageTransformation CreateImageTransformation(this ImageTransformationType imageTransformationType, string customTransformationType, int width, int height)
        {
            Contract.Requires<ArgumentOutOfRangeException>(width > 0 || imageTransformationType == ImageTransformationType.None);
            Contract.Requires<ArgumentOutOfRangeException>(height > 0 || imageTransformationType == ImageTransformationType.None);
            Contract.Ensures(Contract.Result<IImageTransformation>() != null);

            return ServiceLocator.Resolve<IImageTransformationResolver>()
                .ResolveImageTransformation(imageTransformationType, customTransformationType, width, height);
        }

        [Obsolete("Use the overload that accepts customTransformationType")]
        public static IImageTransformation CreateImageTransformation(this ImageTransformationType imageTransformationType, int width, int height)
        {
            Contract.Requires<ArgumentOutOfRangeException>(width > 0 || imageTransformationType == ImageTransformationType.None);
            Contract.Requires<ArgumentOutOfRangeException>(height > 0 || imageTransformationType == ImageTransformationType.None);
            Contract.Ensures(Contract.Result<IImageTransformation>() != null);

            return imageTransformationType.CreateImageTransformation(String.Empty, width, height);
        }
    }

    [DefaultImplementation(typeof(ImageTransformationResolver))]
    public interface IImageTransformationResolver
    {
        IImageTransformation ResolveImageTransformation(ImageTransformationType imageTransformationType, string customTransformationType, int width, int height);
    }

    public class ImageTransformationResolver : IImageTransformationResolver
    {
        public virtual IImageTransformation ResolveImageTransformation(ImageTransformationType imageTransformationType, string customTransformationType, int width, int height)
        {
            switch (imageTransformationType)
            {
                case ImageTransformationType.None:
                    return ImageTransformation.Null;
                case ImageTransformationType.CentralCrop:
                    return new CentralCropTransformation(width, height);
                case ImageTransformationType.Stretch:
                    return new StretchTransformation(width, height);
                case ImageTransformationType.ScaleToFit:
                    return new ScaleToFitTransformation(width, height);
                case ImageTransformationType.ScaleDownToFit:
                    return new ScaleDownToFitTransformation(width, height);
                case ImageTransformationType.ScaleDownToFill:
                    return new ScaleDownToFillTransformation(width, height);
                case ImageTransformationType.ScaleToFill:
                    return new ScaleToFillTransformation(width, height);
                default:
                    throw new ArgumentException("Invalid ImageTransformationType", "imageTransformationType");
            }
        }
    }
}